home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / DATABASE / OBJ1_2.ZIP;1 / C_FINFO.PRG < prev    next >
Encoding:
Text File  |  1993-01-21  |  7.1 KB  |  211 lines

  1. //*****************************************************************************
  2. // C_FInfo.prg
  3. // File viewer class for OBJECT v2.03
  4. // Copyright (c) 1991, JHK, JHK-Software, Piestany
  5. // Please compile with: /N/M/W/A
  6. //-----------------------------------------------------------------------------
  7.  
  8. #include "InKey.ch"
  9. #include "FileIo.ch"
  10. #include "Object.ch"
  11.  
  12. create class FInfo from Info
  13.   export:
  14.   var BigFile // false
  15.   var FHandle // 0
  16.   var FSize   // 0;
  17.   method New=FInfoNew              //o:New()
  18.   method Init=FInfoInit            //o:Init(FName,WinName,R,C,Rs,Cs,Clr,Shadow)
  19.   method GoodInit=FInfoGoodInit    //o:GoodInit(FName,WinName,R,C,Rs,Cs,CurSize,Clr,Shadow)
  20.   method Print=FInfoPrint          //o:Print()
  21.   method VProcess=FInfoVProcess    //o:VProcess()
  22.   method Done=FInfoDone            //o:Done()
  23.   endclass
  24.  
  25.  
  26. //*****************************************************************************
  27. // FInfo:New() --> self
  28. // initialize new object
  29. //
  30. constructor FInfoNew()
  31.   ::BigFile:= false
  32.   ::FHandle:= 0
  33.   ::FSize:= 0
  34.   return(self)
  35.  
  36.  
  37. //-----------------------------------------------------------------------------
  38. // FInfoShow(FName,R,C,Rs,Cs,Clr,Shadow) --> true/false
  39. // create new file viewer
  40. //
  41. function FInfoShow(FName,R,C,Rs,Cs,Clr,Shadow)
  42.   local FInfo
  43.   default FName to "*.*"
  44.   object FInfo of FInfo
  45.   FInfo:Wrap:=false
  46.   FInfo:super(Info):Init(ResTxt(032)+": "+FName,R,C,Rs,Cs,Clr,Shadow) //init empty window
  47.   FInfo:Top(false)                        //paint empty window
  48.   if Empty(FName:=GetFile(FInfo,FName))   //get file name
  49.     FInfo:Done()
  50.     return(false)
  51.   endif
  52.   PreInit(FInfo,FName)
  53.   FInfo:RCInfo:=""                         //force redraw window
  54.   FInfo:Name:=ResTxt(032)+": "+Fname       //selected file name
  55.   FInfo:Paint()                            //show window name
  56.   return(FInfo:Process())
  57.  
  58.  
  59. //-----------------------------------------------------------------------------
  60. // FInfo::GetFile(cMask) --> cFileName
  61. // get new file name
  62. //
  63. static function GetFile(FInfo,cMask)
  64.   local Ch,Dir,ar:={}
  65.   local object Choice of Choice
  66.   SaveDOut(ResTxt(164))
  67.   AEval((Dir:=Directory(cMask)),{|e|AAdd(ar,PadR(e[1],13)+PadL(NTrim(e[2]),8)+" "+DtoC(e[3])+" "+e[4])})
  68.   if Empty(ar); return(nil); endif
  69.   Choice:FastInit(ResTxt(033)+" "+cMask,FInfo:Row+1,FInfo:Col+2,,ar)
  70.   Ch:=Choice:Process()
  71.   Choice:Done()
  72.   RestDOut()
  73.   if Ch<=0; return(""); endif
  74.   return(Dir[Ch,1])
  75.  
  76.  
  77. //*****************************************************************************
  78. // FInfo:Init(FName,WinName,R,C,Rs,Cs,Clr,Shadow) --> true/false
  79. // open the file, initialize the task.
  80. //
  81. method function FInfoInit(FName,WinName,R,C,Rs,Cs,Clr,Shadow)
  82.   if !PreInit(self,FName); return(false); endif
  83.   ::super(Info):Init(WinName,R,C,Rs,Cs,Clr,Shadow)
  84.   return(true)
  85.  
  86.  
  87. //*****************************************************************************
  88. // FInfo:GoodInit(FName,WinName,R,C,Rs,Cs,CurSize,Clr,Shadow) --> true/false
  89. // open the file, initialize the task.
  90. //
  91. method function FInfoGoodInit(FName,WinName,R,C,Rs,Cs,CurSize,Clr,Shadow)
  92.   if !PreInit(self,FName); return(false); endif
  93.   ::super(Info):GoodInit(WinName,R,C,Rs,Cs,CurSize,Clr,Shadow)
  94.   return(true)
  95.  
  96.  
  97. //*****************************************************************************
  98. // FInfo::PreInit(FName) -->true/false
  99. // open the file,read first block
  100. //
  101. static function PreInit(FInfo,FName)
  102.   local Opened,FHandle
  103.   SaveDOut(ResTxt(163))
  104.   FInfo:FName:=FName
  105.   if (Opened:=((FHandle:=FInfo:FHandle:=FOpen(FName))>=0))
  106.     FInfo:BigFile:=((FInfo:FSize:=FSeek(FHandle,0,FS_END))>15000)
  107.     FSeek(FHandle,0)
  108.     GoodRead(FInfo,false,true,if(FInfo:BigFile,15000,FInfo:FSize))  //may be change SeeTop/SeeBottom
  109.     if !FInfo:BigFile; FClose(FHandle); endif  //small file
  110.     FInfo:SeeTop:=true
  111.     FInfo:SeeBottom:=!FInfo:BigFile
  112.   endif
  113.   RestDOut()
  114.   return(Opened)
  115.  
  116.  
  117. //*****************************************************************************
  118. // FInfo:Print() --> true
  119. // printing all file
  120. //
  121. method function FInfoPrint()
  122.   PrintFile(::FName)
  123.   ::Printed:=true
  124.   return(true)
  125.  
  126.  
  127. //*****************************************************************************
  128. // FInfo:VProcess() --> true
  129. // virtual process
  130. //
  131. method function FInfoVProcess()
  132.   local Ch
  133.   if ::BigFile
  134.     SaveDOut(ResTxt(163))
  135.     repeat
  136.       ::super(Info):VProcess()
  137.       Ch:=LastKey()
  138.       do case
  139.         case Ch==K_UP
  140.           MoveUp(self,+1)
  141.         case Ch==K_DOWN
  142.           MoveDown(self,+1)
  143.         case Ch==K_PGUP
  144.           MoveUp(self,-::TextRow+1)
  145.         case Ch==K_PGDN
  146.           MoveDown(self,::TextRow+(::RowSize-1)-::TextMax)
  147.         case Ch==K_CTRL_PGUP
  148.           FSeek(::FHandle,0)
  149.           GoodRead(self,false,true)
  150.           ::TextRow:=1
  151.         case Ch==K_CTRL_PGDN
  152.           FSeek(::FHandle,-15000,FS_END)
  153.           GoodRead(self,true,false)
  154.           ::TextRow:=::TextMax-::RowSize+1
  155.       endcase
  156.     until Ch==nSwapTask or Ch==K_ESC or Ch==K_CTRL_RET
  157.     RestDOut()
  158.   else
  159.     ::super(Info):VProcess()
  160.   endif
  161.   return(true)
  162.  
  163.  
  164. static function MoveUp(FInfo,Ofs)
  165.   local Count,x
  166.   Count:=Min(FSeek(FInfo:FHandle,0,FS_RELATIVE),10000)
  167.   FInfo:SeeTop:=(x:=FSeek(FInfo:FHandle,-Count,FS_RELATIVE))==0
  168.   FInfo:SeeBottom:=x+15000==FInfo:FSize
  169.   Count-=GoodRead(FInfo,FSeek(FInfo:FHandle,0,FS_RELATIVE)<>0,true)-3
  170.   FInfo:TextRow:=MLCount(SubStr(FInfo:Buff,1,Count),250)-Ofs
  171.   if FInfo:TextRow<1; FInfo:TextRow:=1; endif
  172.   return(true)
  173.  
  174.  
  175. static function MoveDown(FInfo,Ofs)
  176.   local FPos,Count
  177.   FPos:=Min(FSeek(FInfo:FHandle,0,FS_RELATIVE)+10000,FInfo:FSize-15000)
  178.   Count:=FSeek(FInfo:FHandle,0,FS_RELATIVE)+Len(FInfo:Buff)-FPos
  179.   FSeek(FInfo:FHandle,FPos)
  180.   Count-=GoodRead(FInfo,true,FPos+15000<FInfo:FSize)-3
  181.   FInfo:TextRow:=MLCount(SubStr(FInfo:Buff,1,Count),250)+Ofs-FInfo:RowSize
  182.   if FInfo:TextRow+FInfo:RowSize-1>FInfo:TextMax; FInfo:TextRow:=FInfo:TextMax-FInfo:RowSize+1; endif
  183.   return(true)
  184.  
  185.  
  186. static function GoodRead(FInfo,lTopCorection,lBottomCorection,nBytes) //strip truncated lines
  187.   local i,j,x
  188.   default nBytes to 15000
  189.   FInfo:Buff:=FReadStr(FInfo:FHandle,nBytes)
  190.   i:=if(lTopCorection, At(cr_lf,FInfo:Buff)+2, 1)                    //cr_lf begin corection :text will be without first cr_lf
  191.   j:=if(lBottomCorection, RAt(cr_lf,FInfo:Buff)-1, Len(FInfo:Buff))  //cr_lf end corection   :...                  last ...
  192.   FSeek(FInfo:FHandle,-nBytes-1+i,FS_RELATIVE)                       //seek corection: FilePointer (seek) will be keept in begin of text in FInfo:Buff
  193.   FInfo:Buff:=SubStr(FInfo:Buff,i,j-i+1)
  194.   FInfo:TextMax:=MLCount(FInfo:Buff,if(FInfo:Wrap,FInfo:ColSize,250))
  195.   FInfo:SeeBottom:=FSeek(FInfo:FHandle,0,FS_RELATIVE)>=FInfo:FSize-15000
  196.   FInfo:SeeTop:=!FInfo:SeeBottom
  197.   return(i-1) //bytes of top_corection
  198.  
  199.  
  200. //*****************************************************************************
  201. // FInfo:Done() --> true/false
  202. // close file
  203. //
  204. method function FInfoDone()
  205.   returnif !::super(Info):Done(true) with false
  206.   if ::BigFile; FClose(::FHandle); endif
  207.   return(true)
  208.  
  209. //------------------------------------------------------- eof (c)JHK ----------
  210.  
  211.